#include "color.h"
#include "vector4.h"
#include <stdlib.h>
#include <math.h>
#include <cmath>

const color color::White = color(255,255,255,255);
const color color::Red = color(255,0,0,255);
const color color::Green = color(0,255,0,255);
const color color::Blue = color(0,0,255,255);
const color color::Black = color(0,0,0,255);
const color color::Teal = color(0,255,255,255);
const color color::Purple = color(255,0,255,255);
const color color::Yellow = color(255,255,0,255);

color color::FromHLS(int hue, float saturation, float lightness)
{
	float chroma = (1.0f - abs(2.0f * lightness - 1.0f)) * saturation;
		
	float r1, g1, b1 = 0.0f;
		
	float step =  255.0f / 6.0f;
	if(step <= 0.0f)
	{
		r1 = 0.0f;
		g1 = 0.0f;
		b1 = 0.0f;
	}
	float h = hue / step;
	float iH = 0.0f;
	double fH = modff(h, &iH);
	float x = chroma * ( 1.0f - abs((int)iH % 2) + fH - 1.0f );
	if(h < 1) {
		r1 = chroma;
		g1 = x;
		b1 = 0.0f;
	}else if(h < 2) {
		r1 = x;
		g1 = chroma;
		b1 = 0.0f;
	}else if(h < 3) {
		r1 = 0.0f;
		g1 = chroma;
		b1 = x;
	}else if(h < 4) {
		r1 = 0.0f;
		g1 = x;
		b1 = chroma;
	}else if(h < 5) {
		r1 = x;
		g1 = 0.0f;
		b1 = chroma;
	}else{
		r1 = chroma;
		g1 = 0.0f;
		b1 = x;
	}
		
	float m = lightness - 0.5f * chroma;
	return color(r1 + m, g1 + m, b1 + m);
}
color::color(void)
{
	this->r = 255;
	this->g = 255;
	this->b = 255;
	this->a = 255;
}
color::color(const color &a)
{
	this->r = a.r;
	this->g = a.g;
	this->b = a.b;
	this->a = a.a;
}
color::color(const vector4 &vecA)
{
	this->r = (byte)vecA.x*255;
	this->g = (byte)vecA.y*255;
	this->b = (byte)vecA.z*255;
	this->a = (byte)vecA.w*255;
}
/*
color::color(int r, int g, int b, int a)
{
	this->r = (byte)abs(r);
	this->g = (byte)abs(g);
	this->b = (byte)abs(b);
	this->a = (byte)abs(a);
}
color::color(uint r, uint g, uint b, uint a)
{
	this->r = (byte)r;
	this->g = (byte)g;
	this->b = (byte)b;
	this->a = (byte)a;
}*/
color::color(int r, int g, int b, int a)
{
	this->r = (byte)abs(r);
	this->g = (byte)abs(g);
	this->b = (byte)abs(b);
	this->a = (byte)abs(a);
}
color::color(float r, float g, float b, float a)
{
	this->r = (byte)(r * 255);
	this->g = (byte)(g * 255);
	this->b = (byte)(b * 255);
	this->a = (byte)(a * 255);
}
color::~color(void)
{
}